package com.atguigu.interpreter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;

public class Client {

	public static void main(String[] args) throws IOException {
		//读取 表达式
		String expStr ="a+b-c";// getExpStr(); // a+b
		//读取 值
		HashMap<String, Integer> var = new HashMap<>();// getValue(expStr);// var {a=10, b=20}
		var.put("a", 10);
		var.put("b", 2);
		var.put("c", 5);
		
		
		// 计算
		ContextForCalculator calculator = new ContextForCalculator(expStr);

		System.out.println("运算结果：" + expStr + "=" + calculator.run(var));
	}

	// 获得表达式
	public static String getExpStr() throws IOException {
		System.out.print("请输入表达式：");
		return (new BufferedReader(new InputStreamReader(System.in))).readLine();
	}

	// 获得值映射
	public static HashMap<String, Integer> getValue(String expStr) throws IOException {
		HashMap<String, Integer> map = new HashMap<>();

		for (char ch : expStr.toCharArray()) {
			if (ch != '+' && ch != '-') {
				if (!map.containsKey(String.valueOf(ch))) {
					System.out.print("请输入" + String.valueOf(ch) + "的值：");
					String in = (new BufferedReader(new InputStreamReader(System.in))).readLine();
					map.put(String.valueOf(ch), Integer.valueOf(in));
				}
			}
		}

		return map;
	}
}
